<?php

function addPhone($config,$ke){
	$phone = array();
	$db =  new db($config);
	if ($ke>0){
		$que = "SELECT no_hape FROM mn_crew WHERE idCrew = '$ke'";
	}else{
		$que = "SELECT no_hape FROM mn_crew WHERE 1";
	}
	$nomors = $db->query($que);
	//var_dump($nomors);
	foreach($nomors as $nomor){
		if ($nomor['no_hape']!=''){
			$nmr = '0'.$nomor['no_hape'];
			array_push($phone, $nmr);
		}
	}
	return $phone;
}

function addSMS($config,$pesan,$dari,$ke){
	$ukey = $config['ukey'];
	$pkey = $config['pkey'];
	$phones = addPhone($config,$ke);
	$message = "[Mading Idebeda]\nDari : $dari\n\"$pesan\"";
	echo "1.".strlen($message);
	$message = helper::trimMsg($message,105);
	$message = $message."\n\nLihat idebeda.com/inti/mading";	//31
	echo "2.".strlen($message);
	echo $message;
	//die();
	foreach ($phones as $nomor) {
		$url = 'http://zenziva.com/apps/smsapi.php?userkey='.$ukey.'&passkey='.$pkey.'&nohp='.$nomor.'&pesan='.urlencode($message);
		//echo $message."\n\n";
		$curlHandle = curl_init();
		curl_setopt($curlHandle, CURLOPT_URL, $url);
		//curl_setopt($curlHandle, CURLOPT_HEADER, 0);
		curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Codular Sample cURL Request');
		curl_setopt($curlHandle, CURLOPT_TIMEOUT,30);
		$results = curl_exec($curlHandle);
		curl_close($curlHandle);
	}
}

function controller_notes($config) {
	if (isset($_SESSION['logged'])){
		if (isset($_POST['pasang'])){
			//masukkkan
			$date = date('Y-m-d H:i:s', time());
			$dari = $_SESSION['logged']['id'];
			$sender = $_SESSION['logged']['uname'];
			$tujuans = $_POST['ckbox'];
			$pesan = $_POST['pesan'];
			$db =  new db($config);
			//DB
			foreach ($tujuans as $tujuan){
				$que = "INSERT INTO mn_notes (dari, ke, tgl, isi) VALUES ('$dari', '$tujuan', '$date','$pesan')";
				//echo $que;
				$db->exec($que);
				addSMS($config,$pesan,$sender,$tujuan);
				if ($tujuan=='0') break;
			}
			//SMS
			$_SESSION['addsucc'] = "Pesan berhasil dipasang";
			header('Location: '.$config['home_url']."mading");
		}else if (isset($_POST['ulang'])){
			//ubah
			$date = date('Y-m-d H:i:s', time());
			$idm = $_POST['idmading'];
			$dari = $_SESSION['logged']['id'];
			$sender = $_SESSION['logged']['uname'];
			$pesan = $_POST['pesan'];
			$db =  new db($config);
			//DB
			$que = "UPDATE mn_notes SET tgl = '$date', isi = '$pesan' WHERE idNotes = '$idm'";
			//echo $que;
			$db->exec($que);
			//SMS
			addSMS($config,$pesan,$sender,$tujuan);
			$_SESSION['editsucc'] = "Pesan berhasil dipasang ulang";
			header('Location: '.$config['home_url']."mading/dari");
		}else if ((isset($config['ruri'][1]))&&(isset($config['ruri'][2]))&&($config['ruri'][2]=='hapus')){
			//masukkkan
			$idm = $config['ruri'][1];
			$dari = $_SESSION['logged']['id'];
			$db =  new db($config);
			//DB
			$que = "DELETE FROM mn_notes WHERE idNotes = '$idm' AND dari = '$dari'";
			//echo $que;
			$db->exec($que);
			$_SESSION['editsucc'] = "Pesan berhasil dihapus";
			header('Location: '.$config['home_url']."mading/dari");
		}else if ((isset($config['ruri'][1]))&&(isset($config['ruri'][2]))&&($config['ruri'][2]=='ubah')){
			//ubah
			view_notesb($config,array());
		}else if ((isset($config['ruri'][1]))&&($config['ruri'][1]=='dari')){
			//dari
			view_notesd($config,array());
		}else{
			//utama
			view_notes($config,array());
		}
	}else
		header('Location: '.$config['home_url']);
}
